<template>
    <div class="jgzy-page">
        <div class="left">
            <el-card>
                <div class="input">
                    <el-input style="height:40px;" v-model="filterText" class="w-50 m-2" placeholder="请输入"
                        :prefix-icon="Search" />
                </div>
                <div class="select">
                    <el-tree ref="treeRef" class="filter-tree" :data="data" :props="defaultProps" default-expand-all
                        @filter-node-method="filterNode" @node-click="selectNode">
                    </el-tree>
                </div>
            </el-card>
        </div>
        <div class="right">
            <el-card>
                <div class="top">
                    <div>作业范围</div>
                    <div v-if="!isshow"><button @click="edit">编辑</button></div>
                    <div class="editfw" v-else=""><button
                            style="color: white;background-color: #E15536;margin-right: 20px;"
                            @click="tj">提交</button><button @click="quxiao"
                            style="color: #E15536;background-color: #fff;border: 1px solid gainsboro;">取消</button>
                    </div>
                </div>
                <div v-if="isshow">
                    <button
                        style="width: 100px;height: 40px;border: 1px solid #F3917C;background-color: #FFEEEB;color: #E15536;border-radius: 5px;"
                        @click="starthuizhi">开始绘制</button>
                </div>
                <div class="map">
                    <baidu-map :scroll-wheel-zoom="true" @click="addmo" class="bm-view" :center="city" :zoom="zoom">
                        <bm-polygon :path="polygonPath" stroke-color="red" :stroke-opacity="0.3" :stroke-weight="2"
                            :editing="editing" @lineupdate="updatePolygonPath" />
                        <bm-navigation anchor="BMAP_ANCHOR_TOP_RIGHT">
                        </bm-navigation>
                    </baidu-map>
                </div>
            </el-card>
        </div>
    </div>
</template>

<script setup lang="ts">
import { Search } from '@element-plus/icons-vue'
import axios from 'axios';
import { ElTree } from 'element-plus';
import { ref, reactive, Ref, watch, onMounted } from 'vue'

let starthz = ref(false)
// import './Courier.scss'
let zoom = ref(6)
let editing = ref(true)
// 公司筛选
const filterText = ref('')
// interface Tree {
//     id: number
//     label: string
//     children?: Tree[],
//     level: Number,
//     longitude:String,
// }
// 指定他的数据类型
const defaultProps = {
    children: 'children',
    label: 'label',
}
const isshow = ref(false)
// 地图层
let polygonPath: any = ref([
    // { lng: 116.412732, lat: 39.911707 },
    // { lng: 116.39455, lat: 39.910932 },
    // { lng: 116.403461, lat: 39.921336 }
])
const updatePolygonPath = (e) => {
    // console.log(e.target.getPath());
    polygonPath = e.target.getPath()
    // console.log(polygonPath);
}
const addmo = (e) => {
    if (starthz.value) {
        polygonPath.value.push(
            { lng: e.point.lng, lat: e.point.lat }
        )
        // console.log(polygonPath);
    }
}
// 树结构列表
let data = [
    {
        id: 1,
        label: '品达物流集团有限公司',
        level: 1,
        children: [
            {
                id: 4,
                label: '品达上海分公司',
                level: 2,
                children: [
                    {
                        id: 10,
                        label: '徐汇一级转运中心',
                        level: 3,
                        children: [
                            {
                                id: 18,
                                label: '耳机孔违法和刻录机撒旦',
                                level: 4,
                            }
                        ]
                    },
                    {
                        id: 19,
                        label: '长宁一级转运中心',
                        level: 3,

                    },
                    {
                        id: 11,
                        label: '黄埔一级转运中心',
                        level: 3,
                        children: [
                            {
                                id: 20,
                                label: '黄埔小街网点',
                                level: 4,
                                longitude: '113.48067',
                                latitude: '23.18139'
                            }
                        ]
                    },
                ]
            },
            {
                id: 5,
                label: '品达内蒙古分公司',
                level: 2,
                children: [
                    {
                        id: 10,
                        label: '赤峰市一级转运中心',
                        level: 3,
                        children: [
                            {
                                id: 21,
                                label: '敖汉旗二级转运中心',
                                level: 4,
                                children: [
                                    {
                                        id: 22,
                                        label: '新惠镇网点',
                                        level: 5,
                                        longitude: '113.48067',
                                        latitude: '23.18139'
                                    }
                                ]
                            },
                        ]
                    },
                    {
                        id: 11,
                        label: '乌海市一级转运中心',
                        level: 3,
                        children: [
                            {
                                id: 23,
                                label: '乌海网点1',
                                level: 5,
                                longitude: '113.48067',
                                latitude: '23.18139'
                            }
                        ]
                    },
                ]
            },
            {
                id: 6,
                label: '品达河北分公司',
                level: 2,
                children: [
                    {
                        id: 12,
                        label: '廊坊转运中心',
                        level: 3,
                    }
                ]
            },
            {
                id: 7,
                label: '品达北京分公司',
                level: 2,
                children: [
                    {
                        id: 13,
                        label: '西城一级转运中心',
                        level: 3,
                    },
                    {
                        id: 13,
                        label: '昌平转运中心',
                        level: 3,
                    },
                    {
                        id: 14,
                        label: '朝阳一级转运中心',
                        level: 3,
                    },
                    {
                        id: 15,
                        label: '海淀一级转运中心',
                        level: 3,
                    }
                ]
            },
            {
                id: 8,
                label: '品达吉林分公司',
                level: 2,
                children: [
                    {
                        id: 16,
                        label: '品达长春二级转运中心',
                        level: 3,
                    }
                ]
            },
            {
                id: 9,
                label: '品达辽宁分公司',
                level: 2,
                children: [
                    {
                        id: 17,
                        label: '品达沈阳一级转运中心',
                        level: 3,
                    }
                ]
            },
        ],
    }
]
const treeRef = ref<InstanceType<typeof ElTree>>()
// 监听输入框
watch(filterText, (val) => {
    treeRef.value!.filter(val)
})
// 筛选
const filterNode = (value: string, data) => {
    if (!value) return true
    return data.label.includes(value)
}
const edit = () => {
    isshow.value = !isshow.value
    // polygonPath.value = []
    editing.value = true
}

const quxiao = () => {
    polygonPath.value = []
    starthz.value = false
    isshow.value = false
}

const starthuizhi = () => {
    starthz.value = true
}

// 经纬度查询
let longitude = ref('')
let latitude = ref('')
let city = ref('北京')
const selectNode = (e) => {
    if (!e.children) {
        latitude.value = e.latitude
        longitude.value = e.longitude
        city.value = e.label
        zoom.value = 13
    }
}
const getTreeList = async () => {
    let { data } = await axios.get('/jgzy')
    console.log(data);
    data = data.data
}

const tj = () => {
    isshow.value = false
    starthz.value = false
    editing.value = false
}
onMounted(() => {
    getTreeList()
})

</script>

<style lang="scss" scoped>
.jgzy-page {
    width: 100%;
    // height: 100%;
    box-sizing: border-box;
    padding: 10px;
    display: flex;

    .left {
        width: 35%;
        height: 100%;

        .select {
            box-sizing: border-box;
            margin-top: 20px;
            overflow: auto;
        }
    }

    .right {
        width: 65%;
        height: 100%;
        margin-left: 15px;
        font-weight: bold;

        .top {
            height: 50px;
            width: 100%;
            // background-color: aliceblue;
            display: flex;
            justify-content: space-between;

            button {
                width: 90px;
                border-radius: 5px;
                border: 0;
                background-color: #E15536;
                color: white;
                font-size: 14px;
                height: 35px;
            }

            .editfw {}
        }

        .map {
            width: 100%;

            // height: 600px;
            .bm-view {
                width: 100%;
                padding: 20px 0 0 0;
                height: 595px;

                .navigation {
                    margin-top: 80px;
                }
            }
        }
    }
}

/* .el-tree-node__content:hover {
    background-color: rgb(255, 207, 131);
}

.el-tree-node:focus>.el-tree-node__content {
    background-color: rgb(255, 207, 131);
}

.el-tree-node__label {
    font-size: 1.5vh;
}

.el-tree-node__expand-icon {
    color: transparent;
    cursor: default;
}

.left-tree {
    height: 73vh;
    overflow: auto;
}

.left-tree::-webkit-scrollbar {
    width: 10px;
    height: 10px;
}

.left-tree::-webkit-scrollbar-track {
    background: rgb(104, 108, 143);
    border-radius: 10px;
}

.left-tree::-webkit-scrollbar-thumb {
    background: rgb(54, 229, 150);
    border-radius: 10px;
}

.tree-icon {
    margin-right: 1vh;
    margin-left: -2vh;
}

.el-tree {
    background: none;
    color: #fff;
} */
</style>